package cn.jdy.lhkgaj.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.lhkgaj.entity.LhkgajNews;
import cn.jdy.lhkgaj.service.McAdviceService;
import cn.jdy.plugins.entity.Advice;

@Controller
@RequestMapping("/mcadvice")
public class McAdviceCtrl extends BaseCtrl{

	@Autowired
	private McAdviceService adviceService;
	
	//默认列表路径
	private static String viewName = "redirect:/advice/list?s=";
	
	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Advice entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/operate/adviceList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param news
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Advice advice, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", adviceService.query(advice, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", adviceService.count(advice, fromDate, toDate));
			jsonMap.put("iTotalRecords", adviceService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑/查看
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/adviceDetail.jsp");
		Advice datas = this.adviceService.find(id);
		Advice datanewsNews = new Advice();
		if(id==null){
			datanewsNews.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		}
		//mv.addObject("data", id==null||id==0 ? id : this.adviceService.find(id));
		mv.addObject("data", id==null||id==0 ? datanewsNews : datas);
		return mv;
	}
	
	@RequestMapping("/play/{id}")
	public ModelAndView play(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/adviceplay.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.adviceService.find(id));
		return mv;
	}
	/**
	 * 验收金点子
	 * 保存（更新状态）的金点子
	 * @param news
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(Advice advice, Long pid) {
		ModelAndView mv = new ModelAndView();
		try {
//			if(advice.getId()==null) {
//				// 新增
//				User user = getUser();
//				advice.setUserid(user.getId());
//				advice.setUsername(user.getUsername());
//				if(advice.getCreateTime() != null && advice.getCreateTime() != ""){
//					advice.setCreateTime(advice.getCreateTime());
//				}else{
//					advice.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
//				}
////				advice.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
//				this.adviceService.save(advice);
//				mv.setViewName(this.viewName.concat("1"));
//			} else {
				//更新
				if(advice.getFalseCreateTime() == null || advice.getFalseCreateTime() == ""){
					advice.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
				this.adviceService.update(advice);
				mv.setViewName(this.viewName.concat("2"));
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, advice.getId());
//			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return mv;
	}
	
	
	@RequestMapping("/saveAdvice")
	@ResponseBody
	public  Map<String, Object>  saveAdvice(Advice advice, Long pid) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(advice.getId()==null) {
				// 新增
				User user = getUser();
				advice.setUserid(user.getId());
				advice.setUsername(user.getUsername());
				if(advice.getCreateTime() != null && advice.getCreateTime() != ""){
					advice.setFalseCreateTime(advice.getCreateTime());
				}else{
					advice.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
//				advice.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.adviceService.save(advice);
				mv.setViewName(this.viewName.concat("1"));
				jsonMap.put("statusCode", 100);
			} else {
				//更新
				if(advice.getFalseCreateTime() == null || advice.getFalseCreateTime() == ""){
					advice.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
				this.adviceService.update(advice);
				mv.setViewName(this.viewName.concat("2"));
				jsonMap.put("statusCode", 200);
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, advice.getId());
			}
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return jsonMap;
	}
	
	/**
	 * 删除新闻
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.adviceService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理文章缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}
}
